package com.winai.launcher.manager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.bean.LiveDataBean;
import com.winai.launcher.constant.Constant;

import java.util.Observable;

/**
 * @author kangshao
 * @date 2021/1/18
 * @desc Banner的直播通知管理
 */
public class LiveManager extends Observable {
    private Context mContext;
    private static LiveManager mInstance;
    private LiveDataBean mLiveDataBean;

    private int liveState;//直播状态，默认值：Constant.LIVE_STATE；
    private String routerPath;//路径
    public static final int SHOW_WHITE = 2000;//显示白色
    public static final int SHOW_GREEN = 2001;//显示绿色
    public static final int NOT_SHOW = 2002;//不显示
    private Integer shouldShowDialog = NOT_SHOW;//默认不显示
    private long preLiveStartTime, liveEndTime;

    public static synchronized LiveManager getInstance(Context mContext) {
        if (mInstance == null) {
            synchronized (LiveManager.class) {
                if (null == mInstance) {
                    mInstance = new LiveManager(mContext.getApplicationContext());
                }
            }
        }
        return mInstance;
    }

    private LiveManager(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(int liveState, String routerPath, LiveDataBean mLiveDataBean) {
        setLiveState(liveState);
        setRouterPath(routerPath);
        setLiveDataBean(mLiveDataBean);
        if (mLiveDataBean != null) {
            preLiveStartTime = mLiveDataBean.getLiveStartTime();//直播开始时间（毫秒）
            liveEndTime = mLiveDataBean.getLiveEndTime();//直播结束时间（毫秒）
            updateState();
        }
        startTime();
    }

    /**
     * @date 2021/1/18
     * @desc 监听时间是否到达开始时间
     */
    private void startTime() {
        //时钟广播—— 一分钟更新一次
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        mContext.registerReceiver(timeReceiver, filter);
    }

    /**
     * @date 2021/1/18
     * @desc 更新状态
     */
    private void updateState() {
        long currentTime = System.currentTimeMillis();//当前时间（毫秒）
        long liveStartTime = preLiveStartTime + 5 * 60 * 1000;//直播前五分钟的时间点（毫秒）
        LogUtils.d("更新直播状态：currentTime = " + currentTime
                + "；preLiveStartTime = " + preLiveStartTime +
                "；liveStartTime = " + liveStartTime
                + "；liveEndTime = " + liveEndTime
                + "；liveState = " + liveState);

        if ((currentTime >= preLiveStartTime && currentTime < liveStartTime) || liveState == Constant.LIVE_NOT_START) {
            //直播时间>当前时间>=直播前五分钟时间 或者 直播状态为1000。显示白色
            if (shouldShowDialog != SHOW_WHITE) {
                shouldShowDialog = SHOW_WHITE;
            }
        } else if ((currentTime >= liveStartTime && currentTime < liveEndTime) || liveState == Constant.LIVE_START) {
            //直播结束时间>当前时间>=直播时间。或者 直播状态为1001。显示绿色
            if (shouldShowDialog != SHOW_GREEN) {
                shouldShowDialog = SHOW_GREEN;
            }
        } else {
            //否则不显示
            if (shouldShowDialog != NOT_SHOW) {
                shouldShowDialog = NOT_SHOW;
            }
        }
        //发送观察者通知
        setChanged();
        notifyObservers(shouldShowDialog);
    }

    /**
     * 说明：一分钟监听更新一次UI
     */
    private final BroadcastReceiver timeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null && action.equals(Intent.ACTION_TIME_TICK)) {
                updateState();
            }
        }
    };

    public LiveDataBean getLiveDataBean() {
        return mLiveDataBean;
    }

    public void setLiveDataBean(LiveDataBean mLiveDataBean) {
        if (mLiveDataBean == null) {
            LogUtils.e("艾益生广播过来的直播数据为空！");
            return;
        }
        this.mLiveDataBean = mLiveDataBean;
    }

    public int getLiveState() {
        return liveState;
    }

    public void setLiveState(int liveState) {
        this.liveState = liveState;
    }

    public String getRouterPath() {
        return routerPath;
    }

    public void setRouterPath(String routerPath) {
        this.routerPath = routerPath;
    }

    public int getShouldShowDialog() {
        return shouldShowDialog;
    }

    public void setShouldShowDialog(int shouldShowDialog) {
        this.shouldShowDialog = shouldShowDialog;
    }
}
